package com.yycx.starter.fileview.conver;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.util.PoitlIOUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 文档导出工具类
 *
 * @author zyf
 */
public class PoiTlUtils {

    /**
     * 根据模板路径写入文件
     *
     * @param tempPath
     * @param targetPath
     * @param map
     * @return
     */
    public static Boolean writeToFile(String tempPath, String targetPath, Map<String, Object> map) {
        try {
            XWPFTemplate template = XWPFTemplate.compile(tempPath);
            createFile(targetPath);
            template.render(map).writeToFile(targetPath);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据文件流写入文件
     *
     * @param response
     * @param tempPath
     * @param map
     * @return
     */
    public static Boolean write(HttpServletResponse response, String tempPath, String filaName, Map<String, Object> map) {
        try {
            InputStream inputStream = ResourceUtil.getStream(tempPath);
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename=\"" + filaName + "\"");
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            XWPFTemplate template = XWPFTemplate.compile(inputStream);
            template.render(map).writeAndClose(bos);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static Boolean write(HttpServletResponse response, String tempPath, String filaName, Map<String, Object> map, Configure configure) {
        try {
            response.setContentType("application/octet-stream;charset=UTF-8");
            filaName = URLEncoder.encode(filaName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filaName);
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            XWPFTemplate template = XWPFTemplate.compile(tempPath,configure);
            template.render(map).writeAndClose(bos);
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }



    public static Boolean writePdf(HttpServletResponse response,String basePath, String tempPath, String filaName, Map<String, Object> map, Configure configure) {
        try {
            response.setContentType("application/octet-stream;charset=UTF-8");
            filaName = URLEncoder.encode(filaName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filaName);

            //创建文件
            String fileId= IdWorker.getIdStr();
            String fileName =fileId+ ".docx";
            FileUtil.mkdir(basePath);
            String path = basePath+ fileName;
            XWPFTemplate template = XWPFTemplate.compile(tempPath,configure);
            template.render(map).writeToFile(path);

            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            InputStream in =  ResourceUtil.getStream(path);
            Document document = new Document(in);
            document.save(out, SaveFormat.PDF);
            IOUtils.copy(in, out);
            out.flush();
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 创建文件
     */
    public static File createFile(String path) {
        String dir = path.substring(0, path.lastIndexOf("/"));
        File file = new File(dir);
        if (!file.exists()) {
            //创建文件夹
            file.mkdirs();
        }
        File f = new File(path);
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        return f;
    }

}
